#include "FreeRTOS.h"
#include "task.h"

#include "stm32f30x.h"
#include "stm32f30x_gpio.h"
#include "stm32f30x_rcc.h"

//void delay_ms(uint32_t ms)
//{
//	for(; ms > 0; ms--)
//		for(int i = 1000*20; i > 0; i--)
//			__NOP();
//}

void LED_init(void)
{
    GPIO_InitTypeDef        GPIO_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
}

void LED_RESET_TASK(void * argument)
{
  for(;;)
  {
	  while(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13) == Bit_SET)
	  {
		  vTaskDelay(2000);
		  GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
	  }
  }
}

void LED_SET_TASK(void * argument)
{
  for(;;)
  {
	  while(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET)
	  {
		  vTaskDelay(2000);
		  GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
	  }
  }
}

int main(void)
{
  //SystemInit();
  SystemCoreClockUpdate();

  LED_init();
	
  xTaskCreate(LED_RESET_TASK, "LED_RESET_TASK", 128, NULL, 3, NULL);
  xTaskCreate(LED_SET_TASK, "LED_SET_TASK", 128, NULL, 3, NULL);
  vTaskStartScheduler();
	
  return 0;
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
